<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "cdn_config".
 *
 * @property-read array $classArray
 * @property-read array $statusTexts
 * @property string $class 存储名称
 * @property string $appId appId
 * @property string $accessKey accessKey
 * @property string $accessSecret accessSecret
 * @property string $bucket 存储空间(bucket)
 * @property string $endPoint 存储节点(endPoint)
 * @property string $host 存储域名(host)
 * @property int $status 状态
 */
class CdnConfig extends BaseModels
{

    const QiNiu = 'feehi\cdn\QiniuTarget';
    const AliOSS = 'feehi\cdn\AliossTarget';
    const QCloud = 'feehi\cdn\QcloudTarget';
    const NetEase = 'feehi\cdn\NeteaseTarget';

    /**
     * @var array
     */
    public $classArray = [
        self::QiNiu   => '七牛云',
        self::AliOSS  => '阿里云',
        self::QCloud  => '腾讯云',
        self::NetEase => '网易云'
    ];

    /**
     * @var array
     */
    public $statusTexts = [
        0 => '禁用',
        10 => '启用',
    ];

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'cdn_config';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['class','accessKey','accessSecret','bucket','host'], 'required'],
            [['status'], 'integer'],
            [['class', 'appId', 'accessKey', 'bucket'], 'string', 'max' => 100],
            [['accessSecret', 'host'], 'string', 'max' => 255],
            [['endPoint'], 'string', 'max' => 50],
            [['class'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'class' => '存储名称',
            'appId' => 'appId',
            'accessKey' => 'accessKey',
            'accessSecret' => 'accessSecret',
            'bucket' => '存储空间(bucket)',
            'endPoint' => '存储节点(endPoint)',
            'host' => '存储域名(host)',
            'status' => '状态',
        ];
    }


    /**
     * findActive
     * @return array
     */
    public static function findActive()
    {
        $model = self::findOne(['status'=>10]);
        return !empty($model)?$model->toArray():[];
    }

    /**
     * 初始化配置
     * initConfig
     * @param array $config
     * @return array
     */
    protected static function initConfig($config = [])
    {
        $returnConfig['class'] = $config['class'];
        $returnConfig['host'] = $config['host'];

        if($config['class'] == CdnConfig::QiNiu){
            $returnConfig['accessKey'] = $config['accessKey'];
            $returnConfig['secretKey'] = $config['accessSecret'];
            $returnConfig['bucket'] = $config['bucket'];
        }else if($config['class'] == CdnConfig::QCloud){
            $returnConfig['appId'] = $config['appId'];
            $returnConfig['secretId'] = $config['accessKey'];
            $returnConfig['secretKey'] = $config['accessSecret'];
            $returnConfig['region'] = $config['bucket'];
        }else{
            $returnConfig['accessKey'] = $config['accessKey'];
            $returnConfig['accessSecret'] = $config['accessSecret'];
            $returnConfig['endPoint'] = $config['endPoint'];
            $returnConfig['bucket'] = $config['bucket'];
        }
        return $returnConfig;
    }

    /**
     * getTarget
     * @return object|null
     */
    public static function getTarget()
    {
        try {
            $cdnConfig = self::findActive();
            if(empty($cdnConfig)){
                return null;
            }
            return Yii::createObject(self::initConfig($cdnConfig));
        }catch (\Exception $e){
            Yii::error('Get Upload Target Module Fail');
            exceptionFormat($e);
            return null;
        }
    }
}
